clear
set more off

use "$tempdata/FINAL_DATASET.dta", clear

label var crit_atm "$<5km$"

label var P_26 "Right place"
label var P_28 "Received CCT"
label var P_29 "$>1$ trip"

label var P_30 "Received full CCT" 

label var P_31 "Money" 
label var P_32 "Time (min.)" 
label var P_33 "Difficult" 
label var P_34 "Unsafe" 
label var P_35 "Far"

label var P_36_a "Pol. Events" 
label var P_36_b "Comm. Work" 
label var P_36_c "Food/Gifts" 
label var P_36_d "Money" 
label var P_36_e "Vote"

label var womP_67_a "Children"
label var womP_67_b "Elderly"
label var womP_67_c "Chores"
label var womP_67_d "Groceries"
label var womP_67_e "Disabilities"
label var womP_67_f "Maintenance"
label var womP_67_g "Ill"
label var escuchado_cambios "Heard Changes"


global impactvar "treat1 treat2"
global control1 "age i.schooling hhsize married"
global control2 "radio refri cuartos wood_cooking licuador lavadora" 

label var treat1 "Deposits" 
label var treat2 "Deposits $+$ SMS" 
label var works_lwk "Works"
label var hours "Hrs."
label var self "Self Emp."
label var labincome "Lab. Income"
label var spouse_works "Spouse Work"
label var spouse_contrib "Spouse Contr."
la var womP_66_a "Work"
la var womP_66_b "Leave House"
la var womP_66_c "Do with Income"
la var womP_66_d "Buy Things"
la var womP_66_f "Spend Money"
la var womP_66_g "Spouse Exp."
la var womP_66_h "Wear"
la var womP_66_i "Child. Permissions"
la var useATM "ATM usage"
la var account_savings "Savings Acc."
la var pct_savings "\% Saved"
la var savings_family "Family"
la var savings_caja "Savings Box"
la var savings_house "House"
la var loaned "Made Loans"
la var P_104_a "Food (HH)"
la var P_104_b "Food"
la var P_104_c "Tobacco"
la var P_104_d "Clothes/Shoes"
la var P_104_e "Housing"
la var P_104_f "Water"
la var P_104_g "Electric"
la var P_104_h "Gas"
la var P_104_i "Cleaning"
la var P_104_j "Healthcare"
la var P_104_k "Transport"
la var P_104_l "Vehicles"
la var P_104_m "Petrol"
la var P_104_n "Telecom"
la var P_104_o "Entertainment"
la var P_104_p "Debt"
la var P_104_q "Loans"
la var P_24 "Days"
la var P_28 "Got CCT"
	replace P_28=. if P_28==99
la var P_29 "Return"
	replace P_29=. if P_29==99
la var P_30 "Full CCT"
	replace P_30=. if P_30==99
la var P_31 "Money"
la var P_32 "Time"
la var P_34 "Insecure"
 

gen any_treatment=(treat1==1 | treat2==1)
label var any_treatment "Prospera Digital"

gen notified=(P_23==1)
	replace notified=. if P_23==99
label var notified "Notified"


label var P_30 "Full CCT" 
label var P_31 "Money" 
label var P_32 "Time" 
label var P_33 "Difficult" 
label var P_34 "Unsafe" 
label var P_35 "Far"

label var P_36_a "Pol. Events" 
label var P_36_b "Comm. Work" 
label var P_36_c "Food/Gifts" 
label var P_36_d "Money" 
label var P_36_e "Vote"


* gen index for in exchange variables 

egen index_exchange=rowtotal(P_36_a P_36_b P_36_c P_36_d P_36_e)
	label var index_exchange "Index"
	
* Table 4: Time and Expenditures needed to collect transfers

************
eststo clear
************

foreach var of varlist P_31 P_32 P_33 P_34 P_35 P_36_a P_36_b P_36_c P_36_d P_36_e index_exchange {
	eststo t1t2_`var': regress `var' treat1 treat2 $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
	test treat1=treat2
	estadd scalar test=r(p)


	eststo anyt_`var': regress `var' any_treatment $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
}	


esttab anyt_P_31 anyt_P_32 anyt_P_33 anyt_P_34 anyt_P_35 ///
using "$results/tables/4A_R_costs.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (any_treatment) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(any_treatment "Prospera Digital")  ///
stats(ymean N, label("MDV (T=0)" "N") fmt(%12.2fc %12.0fc))  ///
label varwidth(22) wrap

* =======
esttab anyt_P_36_a anyt_P_36_b anyt_P_36_c anyt_P_36_d anyt_P_36_e anyt_index_exchange ///
using "$results/tables/4B_R_exchange.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (any_treatment) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(any_treatment "Prospera Digital")  ///
stats(ymean N, label("MDV (T=0)" "N") fmt(%12.2fc %12.0fc))  ///
label varwidth(22) wrap

* Appendix: by treatment type
esttab t1t2_P_31 t1t2_P_32 t1t2_P_33 t1t2_P_34 t1t2_P_35 ///
using "$results/tables/A.1_costs.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (treat1 treat2) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(treat1 "T1" treat2 "T2")  ///
stats(ymean N test , label("MDV (T=0)" "N" "p-value (T1=T2)") fmt(%12.2fc %12.0fc %12.2fc))  ///
label varwidth(22) wrap

* =======
esttab t1t2_P_36_a t1t2_P_36_b t1t2_P_36_c t1t2_P_36_d t1t2_P_36_e t1t2_index_exchange ///
using "$results/tables/A.1_exchange.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (treat1 treat2) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(treat1 "T1" treat2 "T2")  ///
stats(ymean N test , label("MDV (T=0)" "N" "p-value (T1=T2)") fmt(%12.2fc %12.0fc %12.2fc))  ///
label varwidth(22) wrap


* ========================
* Table 5: Success at obtaining transfers
* ========================

************
eststo clear
************

foreach var of varlist P_28  P_30 P_29 P_26  {
	eststo t1t2_`var': regress `var' treat1 treat2 $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
	test treat1=treat2
	estadd scalar test=r(p)


	eststo anyt_`var': regress `var' any_treatment $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
}	

* any treat
esttab anyt_P_28 anyt_P_30 anyt_P_29 anyt_P_26 ///
using "$results/tables/5_R_successtransfers.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (any_treatment) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(any_treatment "Prospera Digital")  ///
stats(ymean N, label("MDV (T=0)" "N") fmt(%12.2fc %12.0fc))  ///
label varwidth(22) wrap

* by t type
esttab t1t2_P_28 t1t2_P_30 t1t2_P_29 t1t2_P_26 ///
using "$results/tables/A.2_successtransfers.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (treat1 treat2) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(treat1 "T1" treat2 "T2")  ///
stats(ymean N test , label("MDV (T=0)" "N" "p-value (T1=T2)") fmt(%12.2fc %12.0fc %12.2fc))  ///
label varwidth(22) wrap

/* I think this is not in paper now
*Table 6a
************
eststo clear
************

foreach var of varlist P_104_a-P_104_i {
	eststo: regress `var' treat1 treat2 $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
	test treat1=treat2
	estadd scalar p_diff = `r(p)'
}	

esttab using "$Results/tables/T_aExpend.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (treat1 treat2)   ///
starl(* 0.1 ** 0.05 *** 0.01)  ///
stats(p_diff ymean N r2, label("T1=T2 (p-value)" "MDV (T=0)" "N" "R-squared") fmt(%12.2fc %12.2fc %12.0fc %12.2fc))  ///
label varwidth(22) wrap

*Table 6b
************
eststo clear
************

foreach var of varlist P_104_j-P_104_q {
	eststo: regress `var' treat1 treat2 $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
	test treat1=treat2
	estadd scalar p_diff = `r(p)'
}	

esttab using "$Results/tables/T_bExpend.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (treat1 treat2)   ///
starl(* 0.1 ** 0.05 *** 0.01)  ///
stats(p_diff ymean N r2, label("T1=T2 (p-value)" "MDV (T=0)" "N" "R-squared") fmt(%12.2fc %12.2fc %12.0fc %12.2fc))  ///
label varwidth(22) wrap

* ==========================================================================================

* ALternative specification: gen any_treatment

*Table 6a
************
eststo clear
************

foreach var of varlist P_104_a-P_104_i {
	eststo: regress `var' any_treatment $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)

}	

esttab using "$Results/TT_aExpend.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (any_treatment)   ///
starl(* 0.1 ** 0.05 *** 0.01)  ///
stats(ymean N r2, label("MDV (T=0)" "N" "R-squared") fmt(%12.2fc %12.0fc %12.2fc))  ///
label varwidth(22) wrap

*Table 6b
************
eststo clear
************

foreach var of varlist P_104_j-P_104_q {
	eststo: regress `var' any_treatment $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)

}	

esttab using "$Results/TT_bExpend.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (any_treatment)   ///
starl(* 0.1 ** 0.05 *** 0.01)  ///
stats(ymean N r2, label("MDV (T=0)" "N" "R-squared") fmt(%12.2fc %12.0fc %12.2fc))  ///
label varwidth(22) wrap
*/

* ==========================================================================================
* Tables using Education

gen edu_high=(P_8>=3)
	replace edu_high=. if P_8==99

* Identify the women that benefited from policy

* in $ terms

gen money_improved=(P_31<P_31_a)
	replace money_improved=. if P_31_a==9999 | P_31==9999
	gen anytreat_money=any_treatment*money_improved

gen time_improved=(P_32<P_32_a)
	replace money_improved=. if P_32_a==9999 | P_32==9999
	gen anytreat_time=any_treatment*time_improved


* ==========================================================================================
* Table 7 heterogeneous effects, success collecting CCTs

replace P_7=0 if P_7==2
gen young=age<40
gen atm_bansefi=P_19==2

************
eststo clear
************

foreach var of varlist P_31 P_32 P_33 P_34 P_35 P_30 P_29 {
	eststo reg_`var'_a: regress `var' i.any_treatment $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)

	eststo reg_`var'_z: regress `var' i.any_treatment##i.crit_atm $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)

	eststo reg_`var'_b: regress `var' i.any_treatment##c.P_15 $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
	
	eststo reg_`var'_c: regress `var' i.any_treatment##i.P_60 $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)

	eststo reg_`var'_d: regress `var' i.any_treatment##i.P_7 $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)

*	eststo reg_`var'_e: regress `var' i.any_treatment##c.age $control1 $control2, cluster(CVE_INEGI)
*	sum `var' if any_treatment==0
*	estadd scalar ymean = r(mean)

	eststo reg_`var'_f: regress `var' i.any_treatment##i.young $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
/*
	eststo reg_`var'_y: regress `var' i.any_treatment##i.atm_bansefi $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
*/
	
}	

esttab reg_P_31_a reg_P_32_a reg_P_33_a reg_P_34_a reg_P_35_a reg_P_30_a reg_P_29_a using "$results/tables/7_TT_HHCCTs.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep (1.any_treatment)   ///
varlabels(1.any_treatment "Treatment" ) starl(* 0.1 ** 0.05 *** 0.01)  ///
noobs label varwidth(22) wrap

esttab reg_P_31_z reg_P_32_z reg_P_33_z reg_P_34_z reg_P_35_z reg_P_30_z reg_P_29_z using "$results/tables/7_TT_HHCCTs.tex", append fragment booktabs b(%12.3fc) se(%12.3fc) se keep (1.any_treatment 1.any_treatment#1.crit_atm) ///
varlabels(1.any_treatment "Treatment" 1.any_treatment "Treatment" ///
1.any_treatment#1.crit_atm "$\times$ ATM $<5km$") starl(* 0.1 ** 0.05 *** 0.01)  ///
nomtitles nonumbers noobs ///
label varwidth(22) wrap
    
esttab reg_P_31_b reg_P_32_b reg_P_33_b reg_P_34_b reg_P_35_b reg_P_30_b reg_P_29_b using "$results/tables/7_TT_HHCCTs.tex", append fragment booktabs b(%12.3fc) se(%12.3fc) se keep (1.any_treatment 1.any_treatment#c.P_15) ///
varlabels(1.any_treatment "Treatment" 1.any_treatment "Treatment" ///
1.any_treatment#c.P_15 "$\times$ Prospera tenure (years)") starl(* 0.1 ** 0.05 *** 0.01)  ///
nomtitles nonumbers noobs ///
label varwidth(22) wrap

esttab reg_P_31_f reg_P_32_f reg_P_33_f reg_P_34_f reg_P_35_f reg_P_30_f reg_P_29_f using "$results/tables/7_TT_HHCCTs.tex", append fragment booktabs b(%12.3fc) se(%12.3fc) se keep (1.any_treatment 1.any_treatment#1.young) ///
varlabels(1.any_treatment "Treatment" 1.any_treatment "Treatment" ///
1.any_treatment#1.young "$\times < 40$ years old") starl(* 0.1 ** 0.05 *** 0.01)  ///
nomtitles nonumbers noobs ///
label varwidth(22) wrap

esttab reg_P_31_c reg_P_32_c reg_P_33_c reg_P_34_c reg_P_35_c reg_P_30_c reg_P_29_c using "$results/tables/7_TT_HHCCTs.tex", append fragment booktabs b(%12.3fc) se(%12.3fc) se keep (1.any_treatment 1.any_treatment#1.P_60) ///
varlabels(1.any_treatment "Treatment" 1.any_treatment "Treatment" ///
1.any_treatment#1.P_60 "$\times$ Married/Partner") starl(* 0.1 ** 0.05 *** 0.01)  ///
nomtitles nonumbers noobs ///
label varwidth(22) wrap
/*
esttab reg_P_31_y reg_P_32_y reg_P_33_y reg_P_34_y reg_P_35_y reg_P_30_y reg_P_29_y using "$Results/TT_HHCCTs.tex", append fragment booktabs b(%12.3fc) se(%12.3fc) se keep (1.any_treatment 1.any_treatment#1.atm_bansefi) ///
varlabels(1.any_treatment "Treatment" 1.any_treatment "Treatment" ///
1.any_treatment#1.atm_bansefi "$\times$ Bansefi ATM") starl(* 0.1 ** 0.05 *** 0.01)  ///
nomtitles nonumbers noobs ///
label varwidth(22) wrap
*/
esttab reg_P_31_d reg_P_32_d reg_P_33_d reg_P_34_d reg_P_35_d reg_P_30_d reg_P_29_d using "$results/tables/7_TT_HHCCTs.tex", append fragment booktabs b(%12.3fc) se(%12.3fc) se keep (1.any_treatment 1.any_treatment#1.P_7) ///
varlabels(1.any_treatment "Treatment" 1.any_treatment "Treatment" ///
1.any_treatment#1.P_7 "$\times$ Literate") starl(* 0.1 ** 0.05 *** 0.01)  ///
nomtitles nonumbers stats(ymean N, label("MDV (T=0)" "N") fmt(%12.2fc %12.0fc))  ///
label varwidth(22) wrap



* comisiones
* 114 comision por consultar saldo
* 115 comision por retiro

************
eststo clear
************

foreach var of varlist P_114_a P_114_b P_114_c P_115_a P_115_b P_115_c {
				replace `var'=. if `var'==999
		gen miss_`var'=`var'==.
}

label var P_114_a "own bank" 
label var P_114_b "diff. bank"
label var P_114_c "shop"
label var P_115_a "own bank"
label var P_115_b "diff. bank"
label var P_115_c "shop"

label var miss_P_114_a "own bank" 
label var miss_P_114_b "diff. bank"
label var miss_P_114_c "shop"
label var miss_P_115_a "own bank"
label var miss_P_115_b "diff. bank"
label var miss_P_115_c "shop"


************
eststo clear
************

foreach var of varlist P_115_a P_115_b P_115_c P_114_a P_114_b P_114_c  {

	eststo `var': regress `var' any_treatment $control1 $control2, cluster(CVE_INEGI)
	sum `var' if any_treatment==0
	estadd scalar ymean = r(mean)
	
}


esttab P_115_a P_115_b P_115_c P_114_a P_114_b P_114_c ///
using "$results/tables/6B_T_comissionsC.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep(any_treatment) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(any_treatment "Prospera Digital")  ///
mgroups("Withdrawal" "Check balance", ///
pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(ymean N, label("MDV (T=0)" "N") fmt(%12.2fc %12.0fc))  ///
label varwidth(22) wrap

************
eststo clear
************

foreach var of varlist P_115_a P_115_b P_115_c P_114_a P_114_b P_114_c {

	eststo `var': regress miss_`var' any_treatment $control1 $control2, cluster(CVE_INEGI)
	sum miss_`var' if any_treatment==0
	estadd scalar ymean = r(mean)
	
}

/*
* =======
esttab P_115_a P_115_b P_115_c P_114_a P_114_b P_114_c ///
using "$results/tables/T_comissions1.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep(any_treatment) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(any_treatment "Prospera Digital")  ///
mgroups("Withdrawal" "Check balance", ///
pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(ymean N, label("MDV (T=0)" "N") fmt(%12.2fc %12.0fc))  ///
label varwidth(22) wrap
*/
* Flipping dummy variables

foreach var of varlist P_115_a P_115_b P_115_c P_114_a P_114_b P_114_c {
	gen `var'_flip=abs(1-miss_`var')
}

label var P_114_a_flip "own bank" 
label var P_114_b_flip "diff. bank"
label var P_114_c_flip "shop"
label var P_115_a_flip "own bank"
label var P_115_b_flip "diff. bank"
label var P_115_c_flip "shop"




************
eststo clear
************

foreach var of varlist P_115_a P_115_b P_115_c P_114_a P_114_b P_114_c {
	
	eststo `var': regress `var'_flip any_treatment $control1 $control2, cluster(CVE_INEGI)
	sum `var'_flip if any_treatment==0
	estadd scalar ymean = r(mean)
	
}

* =======
esttab P_115_a P_115_b P_115_c P_114_a P_114_b P_114_c ///
using "$results/tables/6A_T_comissions1FLIPPED.tex", replace fragment booktabs b(%12.3fc) se(%12.3fc) se keep(any_treatment) ///
starl(* 0.1 ** 0.05 *** 0.01) varlabels(any_treatment "Prospera Digital")  ///
mgroups("Withdrawal" "Check balance", ///
pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(ymean N, label("MDV (T=0)" "N") fmt(%12.2fc %12.0fc))  ///
label varwidth(22) wrap
